package com.estar.assistsurvy.activity;

import java.util.ArrayList;
import java.util.Set;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.SidebarListviewAdapter;
import com.estar.assistsurvy.constant.Constants;
import com.estar.assistsurvy.entity.Item;
import com.estar.assistsurvy.utils.GlideCircleTransform;
import com.estar.assistsurvy.utils.TimeUtils;
import com.estar.assistsurvy.widget.NotScrollListview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import android.widget.ImageView;
import android.widget.TextView;

public class SidebarActivity extends BaseActivity implements OnClickListener,OnItemClickListener{
	private NotScrollListview sidebar_lv;
	private ImageView sidebar_headImg;
	private RequestManager glideRequest;
	private TextView sidebar_name,sidebar_currentTime,sidebar_version;
	private View darkModule;
	private SharedPreferences preference;
	private String headUrl,realName;
	private Intent intent;

	private String[] texts=new String[]{"钱包","关于","常见问题","退出"};
	private int[] imgs=new int[]{R.drawable.icon_wallet,R.drawable.icon_about,
			R.drawable.icon_question,R.drawable.icon_logout};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sidebar);
		init();
		
		sidebar_name.setText(realName);
//		sidebar_currentTime.setText(TimeUtils.getStringDate(createDate));
		sidebar_currentTime.setText(TimeUtils.getCurentTime());
		sidebar_version.setText("版本号 v1.0.0");
		SidebarListviewAdapter adapter=new SidebarListviewAdapter(SidebarActivity.this, getItems());
		sidebar_lv.setAdapter(adapter);
		sidebar_lv.setOnItemClickListener(this);
	}

	private void init(){
		sidebar_lv=find(R.id.sidebar_lv);
		sidebar_headImg=find(R.id.sidebar_headImg);
		
		preference=getSharedPreferences("user", Context.MODE_PRIVATE);
		realName=preference.getString("realName", "");
		preference.getString("createDate", "");
		headUrl=preference.getString("headUrl", "");
		glideRequest = Glide.with(this);
//		String url="http://i0.sinaimg.cn/lx/2012/0411/U3606P8DT20120411102936.jpg";
//		if(headUrl.equals("")){
//			glideRequest.load(R.drawable.icon_head).transform(new GlideCircleTransform(this)).into(sidebar_headImg);
//		}else{
			glideRequest.load(Constants.PIC_PATH+headUrl)
			.diskCacheStrategy(DiskCacheStrategy.ALL)
			.placeholder(R.drawable.icon_head)
			.transform(new GlideCircleTransform(this))
			.into(sidebar_headImg);
//		}
//		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.psb8);
//		bitmap=BitmapUtils.toRoundBitmap(bitmap);
//		sidebar_headImg.setImageBitmap(bitmap);

		sidebar_currentTime=find(R.id.sidebar_currentTime);
		sidebar_name=find(R.id.sidebar_name);
		sidebar_version=find(R.id.sidebar_version);

		darkModule=find(R.id.darkModule);
		darkModule.setOnClickListener(this);
	}


	private ArrayList<Item> getItems(){
		ArrayList<Item> items=new ArrayList<Item>();
		for(int i=0;i<texts.length;i++){
			Item item=new Item();
			item.setImg(imgs[i]);
			item.setText(texts[i]);
			items.add(item);
		}
		return items;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.darkModule:
			intent=getIntent();
			intent.putExtra("isClick", true);
			setResult(0, intent);
			finish();
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			break;

		default:
			break;
		}
	}
	private long exitTime=0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
			if(System.currentTimeMillis()-exitTime>2000){
				showShortToast("再按一次退出");
			exitTime=System.currentTimeMillis();
			}
			else{
				intent=getIntent();
				intent.putExtra("isClick", true);
				setResult(0, intent);
				finish();
				overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			intent=new Intent(SidebarActivity.this, WalletActivity.class);
			startActivity(intent);
			break;
			
		case 1:
			intent=new Intent(SidebarActivity.this, AboutActivity.class);
			startActivity(intent);
			break;
			
		case 2:
			intent=new Intent(SidebarActivity.this, CommonQuestionActivity.class);
			startActivity(intent);
			break;
			
		case 3:
//			ExitApp();
			CancelUser();
			break;

		default:
			break;
		}
	}
	
	private void CancelUser(){
		new AlertDialog.Builder(SidebarActivity.this,AlertDialog.THEME_HOLO_LIGHT)
		.setTitle("确定退出吗？")
		.setPositiveButton("是", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				preference.edit().clear().commit();
				JPushInterface.setAliasAndTags(getApplicationContext(), "", null,mAliasCallback);
				intent=new Intent(SidebarActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}
		})
		.setNegativeButton("否", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}

		})
		.create()
		.show();
	}
	
	private final TagAliasCallback mAliasCallback = new TagAliasCallback() {  
		@Override  
		public void gotResult(int code, String alias, Set<String> tags) {  
			//	        String logs ;  
			switch (code) {  
			case 0:  
					          String  logs = "Set tag and alias success";  
					            Log.i("wl", logs+"code--->"+code+"----alias--->"+alias);  
				// 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。  
//				SharedPreferences preference=getActivity().getSharedPreferences("alias", Context.MODE_PRIVATE);
//				Editor editor=preference.edit();
//				editor.putString("deviceToken", JPushInterface.getRegistrationID(getActivity().getApplicationContext()));
//				editor.commit();
				break;  
			case 6002:  

				break;  
			default:  
			}   
		}  
	}; 
	
	
}
